﻿Imports System.Web

Namespace DotNetNuke.Modules.ActiveForums
    Public Class ControlUtils
        Friend Function BuildPager(ByVal TabId As Integer, ByVal ModuleId As Integer, ByVal GroupPrefix As String, ByVal ForumPrefix As String, ByVal ForumGroupId As Integer, ByVal ForumID As Integer, ByVal TagId As Integer, ByVal CategoryId As Integer, ByVal OtherPrefix As String, ByVal PageId As Integer, ByVal PageCount As Integer) As String
            If PageCount = 1 Then
                Return String.Empty
            End If
            Dim iMaxPage As Integer = PageId + 2
            If iMaxPage > PageCount Then
                iMaxPage = PageCount
            End If
            Dim i As Integer = 1
            Dim iStart As Integer = 1
            If PageId <= 3 Then
                iStart = 1
                iMaxPage = 5
            Else
                iStart = PageId - 2
                iMaxPage = PageId + 2
            End If

            If iMaxPage > PageCount Then
                iMaxPage = PageCount
            End If
            If iMaxPage = PageCount Then
                iStart = iMaxPage - 4
            End If
            If iStart <= 0 Then
                iStart = 1
            End If
            Dim sb As New Text.StringBuilder
            sb.Append("<div class=""af-pager""><table>")
            sb.Append("<tr>")
            'For x As Integer = 1 To PageCount
            For i = iStart To iMaxPage

                If i = PageId Then
                    sb.Append("<td class=""afpg-current"">")
                    sb.Append("<span>")
                    sb.Append(i)
                    sb.Append("</span>")
                    sb.Append("</td>")
                Else
                    sb.Append("<td class=""afpg-page"">")
                    sb.Append("<a href=""" & BuildUrl(TabId, ModuleId, GroupPrefix, ForumPrefix, ForumGroupId, ForumID, TagId, CategoryId, OtherPrefix, i, -1) & """><span>")
                    sb.Append(i)
                    sb.Append("</span></a>")
                    sb.Append("</td>")
                End If

                If i = PageCount Then
                    Exit For
                End If
            Next
            sb.Append("</tr>")
            sb.Append("</table></div>")
            Return sb.ToString
        End Function
        Friend Function BuildUrl(ByVal TabId As Integer, ByVal ModuleId As Integer, ByVal GroupPrefix As String, ByVal ForumPrefix As String, ByVal ForumGroupId As Integer, ByVal ForumID As Integer, ByVal TagId As Integer, ByVal CategoryId As Integer, ByVal OtherPrefix As String, ByVal PageId As Integer, SocialGroupId As Integer) As String
            Return BuildUrl(TabId, ModuleId, GroupPrefix, ForumPrefix, ForumGroupId, ForumID, -1, String.Empty, TagId, CategoryId, OtherPrefix, PageId, SocialGroupId)
        End Function
        Friend Function BuildUrl(ByVal TabId As Integer, ByVal ModuleId As Integer, ByVal GroupPrefix As String, ByVal ForumPrefix As String, ByVal ForumGroupId As Integer, ByVal ForumID As Integer, ByVal TopicId As Integer, ByVal TopicURL As String, ByVal TagId As Integer, ByVal CategoryId As Integer, ByVal OtherPrefix As String, ByVal PageId As Integer, SocialGroupId As Integer) As String
            Dim _mainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
            Dim params As String() = {}
            If Not _mainSettings.URLRewriteEnabled Or (((String.IsNullOrEmpty(ForumPrefix) And ForumID > 0 And String.IsNullOrEmpty(GroupPrefix)) Or (String.IsNullOrEmpty(ForumPrefix) And String.IsNullOrEmpty(GroupPrefix) And ForumGroupId > 0)) And String.IsNullOrEmpty(OtherPrefix)) Then
                If ForumID > 0 And TopicId = -1 Then
                    params = Utilities.AddParams(ParamKeys.ForumId & "=" & ForumID.ToString, params)
                ElseIf ForumGroupId > 0 And TopicId = -1 Then
                    params = Utilities.AddParams(ParamKeys.GroupId & "=" & ForumGroupId.ToString, params)
                ElseIf TagId > 0 Then
                    'afv=grid&afgt=tags&aftg=
                    params = Utilities.AddParams("afv=grid", params)
                    params = Utilities.AddParams("afgt=tags", params)
                    params = Utilities.AddParams("aftg=" & TagId.ToString, params)


                ElseIf CategoryId > 0 Then
                    params = Utilities.AddParams("act=" & CategoryId.ToString, params)
                ElseIf Not String.IsNullOrEmpty(OtherPrefix) Then
                    params = Utilities.AddParams("afv=grid", params)
                    params = Utilities.AddParams("afgt=" & OtherPrefix, params)
                ElseIf TopicId > 0 Then
                    params = Utilities.AddParams(ParamKeys.TopicId & "=" & TopicId.ToString, params)
                End If
                If PageId > 1 Then
                    params = Utilities.AddParams(ParamKeys.PageId & "=" & PageId, params)
                End If
                If SocialGroupId > 0 Then
                    params = Utilities.AddParams("GroupId=" & SocialGroupId, params)
                End If
                Return Utilities.NavigateUrl(TabId, "", params)
            Else
                Dim sURL As String = String.Empty
                If Not String.IsNullOrEmpty(_mainSettings.PrefixURLBase) Then
                    sURL &= "/" & _mainSettings.PrefixURLBase
                End If
                If Not String.IsNullOrEmpty(GroupPrefix) Then
                    sURL &= "/" & GroupPrefix
                End If
                If Not String.IsNullOrEmpty(ForumPrefix) Then
                    sURL &= "/" & ForumPrefix
                End If
                If Not String.IsNullOrEmpty(TopicURL) Then
                    sURL &= "/" & TopicURL
                End If
                If TagId > 0 Then
                    sURL &= "/" & _mainSettings.PrefixURLTag & "/" & OtherPrefix
                ElseIf CategoryId > 0 Then
                    sURL &= "/" & _mainSettings.PrefixURLCategory & "/" & OtherPrefix
                ElseIf Not String.IsNullOrEmpty(OtherPrefix) And (TagId = -1 Or CategoryId = -1) Then
                    sURL &= "/" & _mainSettings.PrefixURLOther & "/" & OtherPrefix
                End If
                If TopicId > 0 And String.IsNullOrEmpty(TopicURL) Then
                    Return Utilities.NavigateUrl(TabId, "", ParamKeys.TopicId & "=" & TopicId.ToString)
                End If
                If PageId > 1 Then
                    If String.IsNullOrEmpty(sURL) Then
                        Return Utilities.NavigateUrl(TabId, "", ParamKeys.PageId & "=" & PageId)
                    End If
                    sURL &= "/" & PageId.ToString
                End If
                If String.IsNullOrEmpty(sURL) Then
                    Return Utilities.NavigateUrl(TabId)
                End If
                Return sURL & "/"
            End If

        End Function
        Friend Function TopicURL(ByVal row As IDataRecord, ByVal TabId As Integer, ByVal ModuleId As Integer, Optional ByVal PageId As Integer = 1) As String
            Dim _mainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
            Dim sURL As String = String.Empty
            If Not String.IsNullOrEmpty(row("PrefixURL").ToString) And Not String.IsNullOrEmpty(row("URL").ToString) And _mainSettings.URLRewriteEnabled Then
                If Not String.IsNullOrEmpty(_mainSettings.PrefixURLBase) Then
                    sURL &= "/" & _mainSettings.PrefixURLBase
                End If
                If Not String.IsNullOrEmpty(row("GroupPrefixURL").ToString) Then
                    sURL &= "/" & row("GroupPrefixURL").ToString
                End If
                sURL &= "/" & row("PrefixURL").ToString & "/" & row("URL").ToString & "/"
                If PageId > 1 Then
                    sURL &= "/" & PageId.ToString & "/"
                End If
            Else
                If PageId = 1 Then
                    sURL = Utilities.NavigateUrl(TabId, "", New String() {ParamKeys.TopicId & "=" & row("TopicId").ToString})
                Else
                    sURL = Utilities.NavigateUrl(TabId, "", New String() {ParamKeys.TopicId & "=" & row("TopicId").ToString, ParamKeys.PageId & "=" & PageId.ToString})
                End If

            End If
            Return sURL
        End Function
        Friend Function ForumURL(ByVal row As IDataRecord, ByVal TabId As Integer, ByVal ModuleId As Integer, Optional ByVal PageId As Integer = 1) As String
            Return ForumURL(row("GroupPrefixURL").ToString, row("PrefixURL").ToString, Integer.Parse(row("ForumID").ToString), TabId, ModuleId, PageId)
        End Function
        Friend Function ForumURL(ByVal GroupPrefix As String, ByVal ForumPrefix As String, ByVal ForumId As Integer, ByVal TabId As Integer, ByVal ModuleId As Integer, Optional ByVal PageId As Integer = 1) As String
            Dim _mainSettings As SettingsInfo = DataCache.MainSettings(ModuleId)
            Dim sURL As String = String.Empty

            If Not String.IsNullOrEmpty(ForumPrefix) And _mainSettings.URLRewriteEnabled Then
                If Not String.IsNullOrEmpty(_mainSettings.PrefixURLBase) Then
                    sURL &= "/" & _mainSettings.PrefixURLBase
                End If
                If Not String.IsNullOrEmpty(GroupPrefix) Then
                    sURL &= "/" & GroupPrefix
                End If
                sURL &= "/" & ForumPrefix & "/"
                If PageId > 1 Then
                    sURL &= "/" & PageId.ToString & "/"
                End If
            Else
                If PageId = 1 Then
                    sURL = Utilities.NavigateUrl(TabId, "", New String() {ParamKeys.ForumId & "=" & ForumId.ToString})
                Else
                    sURL = Utilities.NavigateUrl(TabId, "", New String() {ParamKeys.ForumId & "=" & ForumId.ToString, ParamKeys.PageId & "=" & PageId.ToString})
                End If

            End If
            Return sURL
        End Function
        Friend Function TopicState(ByVal row As IDataRecord) As String
            Dim states As String = String.Empty
            If CBool(row("IsLocked").ToString) Then
                states &= "<span class=""af-locked""></span>"
            End If
            If CBool(row("IsPinned").ToString) Then
                states &= "<span class=""af-pinned""></span>"
            End If
            Select Case Integer.Parse(row("StatusId").ToString)
                Case 0
                    states &= "<span class=""af-status0""></span>"
                Case 1
                    states &= "<span class=""af-status1""></span>"
                Case 3
                    states &= "<span class=""af-status3""></span>"
            End Select
            Return states
        End Function
        Friend Function Pager(ByVal RecordCount As Integer, ByVal PageSize As Integer, ByVal CurrentPage As Integer, ByVal TabId As Integer) As String
            Return String.Empty
        End Function
    End Class
End Namespace
